﻿using System;
using System.Collections;
using System.Configuration;
using System.Diagnostics;
using System.Web;
using Elmah;

namespace OrbitOne.Elmah.HttpModule
{
    public class O1ErrorFilterModule : ErrorFilterModule
    {

        public override void Init(HttpApplication application)
        {
            if (application == null)
            {
                Trace.Write("application was null");
                return;
            }
                

            ErrorFilterConfiguration config = (ErrorFilterConfiguration)ConfigurationSettings.GetConfig("OrbitOne.Elmah/errorFilter");

            if (config == null)
                return;

            assert = config.Assertion;

            ICollection modules = HttpModuleRegistry.GetModules(application);
            if (modules is HttpModuleCollection)
            {
                int count = modules.Count;
                for (int i = 0; i < count; i++)
                {
                    IHttpModule module = (modules as HttpModuleCollection).Get(i);
                    IExceptionFiltering filtering = module as IExceptionFiltering;

                    if (filtering != null)
                        filtering.Filtering += new ExceptionFilterEventHandler(OnErrorModuleFiltering);

                }
            }
            else
            {
                foreach (IHttpModule module in modules)
                {
                    IExceptionFiltering filtering = module as IExceptionFiltering;

                    if (filtering != null)
                        filtering.Filtering += new ExceptionFilterEventHandler(OnErrorModuleFiltering);
                }
            }
        }
    }
}
